<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-remove"></i></button>
            <h4 class="modal-title">Import ${app} Database</h4>
        </div>
        <div class="modal-body" id="modal-text">
            <form id="import_database_form" enctype="multipart/form-data" method="post" name="import_database_form">
                <input type="hidden" id="import_app" name="import_app" value="${app.lower()}" />
                % if app in ('PlexWatch', 'Plexivity'):
                <p class="help-block">
                    <%
                        v = ''
                        if app == 'PlexWatch':
                            v = '0.3.2'
                        elif app == 'Plexivity':
                            v = '0.9.8'
                    %>
                    <strong>Please ensure your ${app} database is at version ${v} or higher.</strong>
                </p>
                % endif
                <div class="form-group">
                    <label for="import_database_file">Option 1: Upload a Database File</label>
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="input-group">
                                <label for="import_database_file" class="input-group-btn">
                                    <span class="btn btn-form">Upload</span>
                                    <input type="file" style="display: none;" id="import_database_file" name="import_database_file" required>
                                </label>
                                <input id="import_database_file_name" type="text" class="form-control" placeholder="tautulli.db" disabled>
                            </div>
                        </div>
                    </div>
                    <p class="help-block">Upload the ${app} database file you wish to import (max file size is 1GB).</p>
                </div>
                <div class="form-group">
                    <label for="import_database_path">Option 2: Browse for a Database File</label>
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="input-group">
                                <span class="input-group-btn">
                                    <button class="btn btn-form" type="button" id="import_database_path_browse" data-toggle="browse" data-description="Database File" data-filter=".db" data-target="#import_database_path">Browse</button>
                                </span>
                                <input type="text" class="form-control" id="import_database_path" name="import_database_path" value="" placeholder="tautulli.db" required disabled>
                            </div>
                        </div>
                    </div>
                    <p class="help-block">Browse for the ${app} database file you wish to import.</p>
                </div>
                % if app == 'Tautulli':
                <div class="form-group">
                    <label for="table_name">Import Method</label>
                    <div class="row">
                        <div class="col-xs-4">
                            <select class="form-control" id="import_method" name="import_method">
                                <option value="merge">Merge</option>
                                <option value="overwrite">Overwrite</option>
                            </select>
                        </div>
                    </div>
                    <p class="help-block">Select how you would like to import the Tautulli history.</p>
                    <ul class="help-block" style="padding-inline-start: 15px;">
                        <li><strong>Merge</strong> will add all history and remove any duplicates from the imported database into the current database.</li>
                        <li><strong>Overwrite</strong> will replace all history in the current database with the imported database.</li>
                    </ul>
                </div>
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name="import_backup_db" id="import_backup_db" value="1" checked> Backup Current Database
                    </label>
                    <p class="help-block">Automatically create a backup of the current database before importing.</p>
                </div>
                <div class="form-group">
                    <label>Import Notes</label>
                    <p class="help-block">The following data will also be imported:</p>
                    <ul class="help-block" style="padding-inline-start: 15px;">
                        <li>Libraries and Users</li>
                        <li>Notification / Newsletter Agents</li>
                        <li>Registered Mobile Devices</li>
                    </ul>
                </div>
                % else:
                <div class="form-group">
                    <label for="import_table_name">Table Name</label>
                    <div class="row">
                        <div class="col-xs-4">
                            <select class="form-control" id="import_table_name" name="import_table_name">
                                <option value="processed">Processed</option>
                                <option value="grouped">Grouped</option>
                            </select>
                        </div>
                    </div>
                    <p class="help-block">Select the table name from which you wish to import. Only import one of these, importing both will result in duplicated data.</p>
                </div>
                <div class="form-group">
                    <label for="import_ignore_interval">Ignore Interval</label>
                    <div class="row">
                        <div class="col-xs-2">
                            <input type="text" class="form-control" id="import_ignore_interval" name="import_ignore_interval" value="120" required>
                        </div>
                    </div>
                    <p class="help-block">Enter the minimum duration (in seconds) an item must have been active for. Set to 0 to import all.</p>
                </div>
                % endif
            </form>
        </div>
        <div class="modal-footer">
            <div>
                <span id="status-message" style="padding-right: 25px;"></span>
                <input type="button" id="import_db" class="btn btn-bright" value="Import">
            </div>
        </div>
    </div>
</div>
<script>
    $("#import_database_file").change(function() {
        if ($(this)[0].files[0]) {
            $("#import_database_file_name").val($(this)[0].files[0].name);
        }
    });

    $("#import_db").click(function() {
        $(this).prop('disabled', true);

        var app = $("#import_app").val();
        var database_file = $("#import_database_file")[0].files[0];
        var database_path = $("#import_database_path").val();
        var method = $("#import_method").val();
        var backup = $("#import_backup_db").is(':checked');
        var table_name = $("#import_table_name").val();
        var ignore_interval = $("#import_ignore_interval").val();

        var content_type;
        var process_data;
        var data;

        if (database_file) {
            content_type = false;
            process_data = false;
            data = new FormData();
            data.append('app', app);
            data.append('database_file', database_file);
            data.append('method', method);
            data.append('backup', backup);
            data.append('table_name', table_name);
            data.append('ignore_interval', ignore_interval);
        } else {
            content_type = 'application/x-www-form-urlencoded; charset=UTF-8';
            process_data = true;
            data = {
                app: app,
                database_path: database_path,
                method: method,
                backup: backup,
                table_name: table_name,
                ignore_interval: ignore_interval
            }
        }

        if (database_file) {
            $("#status-message").html('<i class="fa fa-fw fa-spin fa-refresh"></i>&nbsp; Uploading database file...');
        } else {
            $("#status-message").html('<i class="fa fa-fw fa-spin fa-refresh"></i>');
        }

        $.ajax({
            url: 'import_database',
            type: 'POST',
            data: data,
            cache: false,
            async: true,
            contentType: content_type,
            processData: process_data,
            success: function(data) {
                var msg;
                if (data.result === 'success') {
                    msg = "<i class='fa fa-check'></i>&nbsp; " + data.message;
                } else {
                    msg = "<i class='fa fa-exclamation-triangle'></i>&nbsp; " + data.message;
                }
                $("#status-message").html(msg);
                $("#import_database_file").val(null);
                $("#import_database_file_name").val('');
                $("#import_database_path").val('');
            },
            error: function (xhr) {
                var msg = "<i class='fa fa-exclamation-triangle'></i>&nbsp; Error (" + xhr.status + "): ";
                if (xhr.status === 413) {
                    msg += "file is too large to upload"
                } else {
                    msg += 'try again'
                }
                $("#status-message").html(msg);
            },
            complete: function(xhr) {
                $("#import_db").prop('disabled', false);
            }
        });
    });
</script>